using System;
using System.Collections.Generic;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Net;
using Microsoft.Xna.Framework.Storage;
using NovodexWrapper;

/// <summary>
/// this is a background object originally intended to be a tree but is now a pyramid
/// </summary>
/// 
namespace marbletrack
{
    public class TreeObject : Obstacle
    {
        protected float angleMod = 1;
        protected bool FlagSlowDown = false;

        public TreeObject(Vector3 modelPosition, ModelClass model)
        {
            model_ = model;
            SetupVariables(Vector3.Zero, modelPosition);

        }

        internal override void SetupVariables(Vector3 rotation, Vector3 modelPosition)
        {
            bounceAngle = (float)(Game1.Ran.Next(0, 360));
            modelScale_ = 1f;
            active = false;
            mouseClickTolerance =Game1.SQUARESIZE*10;//size of object or radius of area in which to click
            nextModelPosition_ =modelPosition_= modelPosition;
            // Setup orientation variables
            modelRotation_ = rotation;
            // Load model
            height = modelPosition.Y;
            CorrectPosition_ = Matrix.CreateTranslation(new Vector3(Game1.SQUARESIZE * 0f, 0, Game1.SQUARESIZE * 0f));

            //Create Physics
            transformMatrix_ = ApplyTransformations();
            trackActor_ = physics.CreateTriangleMeshActor(model_.MeshVertexBuffers[0], model_.MeshIndexBuffers[0], transformMatrix_, 0, true, model_.FlipNormalsFlag);
        }

        public override void Update()
        {
            //Update the physics
            trackActor_.GlobalPose = MathConversion.ToNxMat34(transformMatrix_);

            //animation when clicked on
            if (active)
            {
                //model bounces and spins faster then spits out balls then spins slower
                MakeItBounce(angleMod);
                if (!FlagSlowDown)
                {
                    CreateRotationY(angleMod += 0.01f);
                    
                    if (angleMod > 10)
                    {
                        Game1.CreateParticles(modelPosition_ + Vector3.UnitY * 5 * Game1.SQUARESIZE, new Vector3(1, -1, 1));
                        FlagSlowDown = true;
                    }
                }
                else
                {
                    CreateRotationY(angleMod -= 0.01f);
                    if (angleMod < 2)
                    {
                        active = false;
                        FlagSlowDown = false;
                    }
                }
            }
            else
            {
                //aniamtion when not cliked on
                CreateRotationY(angleMod);
            }
        }

        public override void Draw()
        {
            transformMatrix_ = ApplyTransformations();
            model_.Draw(transformMatrix_);
        }
    }
}